package com.smscenter.server.core.response;

import java.util.HashMap;
import  java.util.Map;

import com.zeus.base.smscenter.common.util.XMLParser;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;


/**
 * Created by Administrator on 2016-02-20.
 */
public class WelinkReponse {

    private static WelinkReponse instance = new WelinkReponse();

	private Map<String,String> parseXML(String xml) {
		Map<String,String> map = new HashMap();
		map.put("state", "-9999");
		map.put("msg", "短信系统解析报文异常错误");
		try {
			// 解析返回XML字符串
			XMLParser parser = XMLParser.getInstance();
			parser.parse(xml);
			NodeList rootList = parser.getList(parser.getDoc(), "CSubmitState");
			for (int i = 0; i < rootList.getLength(); i++) {
				Node root = rootList.item(i);
				String state = parser.getString(root, "State");// 状态码
				String msg = parser.getString(root, "MsgState");// 提示信息
				map.put("state", state);
				map.put("msg", msg);
				if ("0".equals(state)) {//为了和最后返回对上
					map.put("msg", "SUCCESS");
				}
			}
		} catch (Exception e) {
			try {
				throw new Exception("解析XML失败");
			} catch (Exception e1) {
				e1.printStackTrace();
				return map;
			}
		}
		return map;
	}

    public static WelinkReponse getInstance() {
        return instance;
    }

    public String getResponseMessage(String code){
    	Map<String,String> map = this.parseXML(code);
        return map.get("msg");
    }

}
